package com.sshtools.cmd.action;

import com.jcraft.jsch.Session;
import com.sshtools.utils.SSHUtils;
import com.sshtools.view.model.ServerModel;

import tsif.component.log.Log;

/**
 * SSH服务器任务
 */
public abstract class ServerTask implements Runnable {
	public static final int code_error = 1;
	public static final int code_normal = 0;

	protected final ServerModel server;

	public ServerTask(ServerModel server) {
		this.server = server;
	}

	@Override
	public void run() {
		try {
			Log.info("任务开始! " + this.getClass() + " " + server);
			// 执行连接
			Session session = SSHUtils.connect(server.getHost(), server.getPort(), //
					server.getUsername(), //
					server.getPassword(), server.getKeyFile(), //
					server.getTimeout());

			// 执行操作
			execute(session);

			// 关闭操作
			session.disconnect();

			Log.info("任务完成! " + this.getClass() + " " + server);
		} catch (Exception e) {
			Log.error("任务错误! " + this.getClass() + " " + server, e);
		}
	}

	/** 执行 **/
	protected abstract void execute(Session session) throws Exception;

}
